import authCenter from './auth-center';
import cmsCenter from './cms-center';
import institutionSchoolCenter from './institution-school-center'
import materialCenter from './material-center'
import tenantCenter from './tenant-center'
import trainProjectCenter from './train-project-center'
import uploadCenter from './upload-center'
import userCenter from './user-center';
import userHub from './user-hub'
import wandRest from './wand-rest'
import xmsCenter from './xms-center';

const arr = [
    authCenter, 
    cmsCenter, 
    institutionSchoolCenter, 
    materialCenter, 
    tenantCenter, 
    trainProjectCenter, 
    uploadCenter, 
    userCenter, 
    userHub, 
    wandRest, 
    xmsCenter
];

/**
 * 先运行时，后边改成编译时
 * 解析portal中是否存在相关的 name 和 uri
 */
function checkPortals() {
    let uris = [];
    let msg = '';
    let allPortals = arr.reduce((res, group) => {
        for (let key in group) {
            let obj = group[key];
            let idx = uris.findIndex((item) => {
                if (key === item.key) {
                    msg = `PortalCheckError: the portal name "${key}" has already been declared.`;
                } else if (obj.uri === item.uri) {
                    msg = `PortalCheckError: "${key}" and "${item.key}" has the same uri "${item.uri}" please Keep the portal uri unique.`;
                }
                return !!msg;
            });
            if (idx != -1) {
                throw new Error(msg || '"checkPortals" 校验出错！');
            } else {
                uris.push({
                    key,
                    uri: obj.uri
                });
            }
            res[key] = obj;
        }
        return res;
    }, {});
  return allPortals;
}

export default {
    ...checkPortals()
};
